﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class ORoleDAL
    {
        public static bool Add(ORoleInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_Add", obj.RoleName, obj.Remark, obj.IsAgent);
            int roleid = Convert.ToInt32(DB.ExecuteScalar(dbCommand));

            return roleid>0;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="RoleName"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public static bool Add(ORoleInfo obj,string FunIds)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_Add", obj.RoleName, obj.Remark,obj.IsAgent);
            int roleid=Convert.ToInt32(DB.ExecuteScalar(dbCommand));

            return AddFunction(roleid, FunIds);
        }
        /// <summary>
        /// 添加角色权限信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="FunId"></param>
        /// <returns></returns>
        public static bool AddFunction(int RoleId, string FunId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_AddFunction", RoleId, FunId);
            DB.ExecuteNonQuery(dbCommand);
            return true;
        }
        /// <summary>
        /// 删除角色，将会删除此角色对应的所有权限信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static bool Delete(int RoleId)
        {
            try
            {
                Database DB = DatabaseFactory.CreateDatabase();

                DbCommand dbCommand = DB.GetStoredProcCommand("ORole_Delete", RoleId);
                DB.ExecuteNonQuery(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
          
        }
        /// <summary>
        /// 删除角色对应的所有权限信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static bool DeleteRoleFunction(int RoleId)
        {
            try
            {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_DeleteRoleFunction", RoleId);
            DB.ExecuteNonQuery(dbCommand);
            return true;
           }
            catch
            {
                return false;
            }
        }
        /// <summary>
        /// 获取所有的角色信息
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAll()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();				
            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_GetAll");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
	    }
        /// <summary>
        /// 根据角色ID获取角色信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static IDataReader GetById(int RoleId)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_GetById", RoleId);
            return DB.ExecuteReader(dbCommand);
	    }

        public static bool Update(ORoleInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_Update", obj.RoleId, obj.RoleName, obj.Remark);
            try
            {
                DB.ExecuteNonQuery(dbCommand);              
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <param name="RoleName"></param>
        /// <param name="Remark"></param>
        /// <returns></returns>
        public static bool Update(ORoleInfo obj,string FunIds)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_Update", obj.RoleId, obj.RoleName, obj.Remark);
            try
            {
                DB.ExecuteNonQuery(dbCommand);
                DeleteRoleFunction(obj.RoleId);
                AddFunction(obj.RoleId, FunIds);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public 	static  IDataReader GetRoleFunctionByRoleId(int RoleId)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_GetRoleFunctionByRoleId", RoleId);
            return DB.ExecuteReader(dbCommand);
	    }

        /// <summary>
        /// 根据角色ID获取角色对应菜单信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static IDataReader GetRoleMenu(int RoleId)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_GetRoleMenu", RoleId);
            return DB.ExecuteReader(dbCommand);
        }

        /// <summary>
        /// 获取角色对应的菜单权限信息返回OMenuPermissionInfo类
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public static IDataReader GetPermissionByRoleId(int RoleId)
        {
             Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("ORole_GetPermissionByRoleId", RoleId);
            return DB.ExecuteReader(dbCommand);
        }
    }
}
